package com.lottery.game.core.modules.service.app;

import com.lottery.game.core.modules.entity.user.GameAgent;
import com.lottery.game.core.modules.entity.user.GameAgentRule;
import com.lottery.game.core.modules.entity.user.GameDividend;
import com.lottery.game.core.modules.entity.user.GameUserMoneyChange;
import com.lottery.game.core.modules.mapper.GameAgentDao;
import com.lottery.game.core.modules.mapper.GameAgentRuleDao;
import com.lottery.game.core.modules.mapper.GameDividendDao;
import com.lottery.game.core.modules.mapper.GameUserMoneyChangeDao;
import com.lottery.game.core.modules.mapper.OperationalSetDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GameAgentService {

	@Autowired
	private GameAgentDao gameAgentDao;

	@Autowired
	private GameAgentRuleDao gameAgentRuleDao;

	@Autowired
	private GameDividendDao gameDividendDao;

	@Autowired
	private GameUserMoneyChangeDao gameUserMoneyChangeDao;

	@Autowired
  private OperationalSetDao operationalSetDao;



	public GameAgent queryUser(String userId) {
		return this.gameAgentDao.get(userId);
	}

    /**
     * 代理规则
     *
     * @return
     */
    public GameAgentRule queryGameAgentRule() {
        return this.gameAgentRuleDao.get("1");
    }

    /**
     * 代理分红
     *
     * @param agentId
     * @return
     */
    public List<GameDividend> queryDividendByAgentId(String agentId) {
        GameDividend gameDividend = new GameDividend();
        gameDividend.setAgentId(agentId);
        return this.gameDividendDao.findList(gameDividend);
    }

    public List<GameAgent> findList(GameAgent gameAgent) {
        return this.gameAgentDao.findList(gameAgent);
    }

    /**
     * 查询代理团队
     *
     * @param agentId 代理人Id
     * @return
     */
    public List<GameAgent> queryGameAgentTeam(String agentId) {
        GameAgent gameAgent = new GameAgent();
        gameAgent.setSuperiors(agentId);
        gameAgent.setDelFlag("0");
        return this.gameAgentDao.findList(gameAgent);
    }

    /**
     * 统计多用户盈亏 投注流水 充值 以及余额总值
     *
     * @param agentList 旗下代理
     * @return
     */
    public Map<String, Double> getAgentStatistics(List<GameAgent> agentList, String beginDate, String endDate, String roomId) {
        List<String> gameUserIdList = new ArrayList<String>();
        Double balanceCount = 0d;
        for (GameAgent gameAgent : agentList) {
            gameUserIdList.add(gameAgent.getId());
            String balance = "0";
            if (!StringUtils.isEmpty(gameAgent.getBalance()) && gameAgent.getBalance().trim().length() > 0) {
                balance = gameAgent.getBalance().trim();
            }
            balanceCount = balanceCount + Double.parseDouble(balance);
        }
        Map<String, Double> result = new HashMap<String, Double>(16);
        result.put("balanceCount", balanceCount);
        //投注奖金记录
        List<GameUserMoneyChange> gameUserMoneyChanges = new ArrayList<GameUserMoneyChange>();
        //充值记录
        List<GameUserMoneyChange> gameUserMoneyChanges2 = new ArrayList<GameUserMoneyChange>();
        if (gameUserIdList != null && gameUserIdList.size() > 0) {
            gameUserMoneyChanges = this.gameUserMoneyChangeDao.findSimpleList(Arrays.asList("bet", "bonus", "cancelBonus"), gameUserIdList, beginDate, endDate, roomId);
            gameUserMoneyChanges2 = this.gameUserMoneyChangeDao.findSimpleList(Arrays.asList("recharge"), gameUserIdList, beginDate, endDate, null);
        }
        gameUserMoneyChanges.addAll(gameUserMoneyChanges2);
        return statistics(result, gameUserMoneyChanges);
    }

    /**
     * 统计某用户盈亏 投注流水 充值
     *
     * @param gameUserId
     * @return
     */
    public Map<String, Double> getAgentStatistics(String gameUserId, String beginDate, String endDate, String roomId) {
        List<String> list = new ArrayList<String>();
        list.add(gameUserId);
        List<GameUserMoneyChange> gameUserMoneyChanges = this.gameUserMoneyChangeDao.findSimpleList(Arrays.asList("bet", "bonus", "cancelBonus"), list, beginDate, endDate, roomId);
        List<GameUserMoneyChange> gameUserMoneyChanges2 = this.gameUserMoneyChangeDao.findSimpleList(Arrays.asList("recharge"), list, beginDate, endDate, roomId);
        gameUserMoneyChanges.addAll(gameUserMoneyChanges2);
        return statistics(null, gameUserMoneyChanges);
    }

    /**
     * 汇总盈亏流水充值结果
     *
     * @param result
     * @param gameUserMoneyChanges
     * @return
     */
    private Map<String, Double> statistics(Map<String, Double> result, List<GameUserMoneyChange> gameUserMoneyChanges) {
        if (result == null) {
            result = new HashMap<String, Double>(16);
        }
        result.put("pouringWater", 0d);
        result.put("recharge", 0d);
        result.put("profit", 0d);
        result.put("bet", 0d);
        result.put("bonus", 0d);
        if (gameUserMoneyChanges != null && gameUserMoneyChanges.size() > 0) {
            for (GameUserMoneyChange gameUserMoneyChange : gameUserMoneyChanges) {
                String type = gameUserMoneyChange.getBetType();
                Double changeAmt = Double.parseDouble(gameUserMoneyChange.getChangeamt());
                Double newAmount = 0d;
                if ("bet".equals(type)) {
                    //投注
                    newAmount = result.get("bet") + changeAmt;
                    result.put("bet", newAmount);
                } else if ("recharge".equals(type)) {
                    //充值
                    newAmount = result.get("recharge") + changeAmt;
                    result.put("recharge", newAmount);
                } else if ("bonus".equals(type) || "cancelBonus".equals(type)) {
                    //中奖或手动开奖返回
                    newAmount = result.get("bonus") + changeAmt;
                    result.put("bonus", newAmount);
                }
            }
        }
        Double profit = result.get("bonus") + result.get("bet");
        result.put("profit", profit);
        result.put("pouringWater", Math.abs(result.get("bet")));
        return result;
    }

    /**
     * 汇总盈亏流水充值结果
     *
     * @param result
     * @param result2
     * @return
     */
    public Map<String, Double> statistics(Map<String, Double> result, Map<String, Double> result2) {
        if (result == null || result.size()==0) {
            result = new HashMap<>(16);
            result.put("pouringWater", 0d);
            result.put("recharge", 0d);
            result.put("profit", 0d);
            result.put("bet", 0d);
            result.put("bonus", 0d);
        }
        for (Map.Entry<String, Double> entry : result2.entrySet()) {
            if (result.containsKey(entry.getKey())) {
                Double value = entry.getValue() + result.get(entry.getKey());
                result.put(entry.getKey(), value);
            }
        }
        return result;
    }

    /**
     * 查询分红明细
     * @param id
     * @return
     */
    public GameDividend queryGameDividend(String id) {
        return this.gameDividendDao.get(id);
    }


    public String getLogoUrl() throws Exception {
        Map<String,String> map = this.operationalSetDao.queryOperationalSetInfo();
        String logoUrl = map.get("website_photo");
        return logoUrl;
    }


}
